package com.tutu.controller.backend;

import com.tutu.common.DataTableFactory;
import com.tutu.controller.common.CommonController;
import com.tutu.service.CodeDictService;
import com.tutu.service.LogInfoService;
import com.tutu.utils.WebUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * 参数管理
 */
@Controller
@RequestMapping(value = "backend/codeDict")
public class CodeDictController extends CommonController {

    @Autowired
    CodeDictService codeDictService;

    @Autowired
    LogInfoService logInfoService;

    @RequestMapping(value = "/index")
    public String index() {
        return "backend/codeDict/list";
    }

    @RequestMapping("/list")
    public void list(HttpServletResponse response, String type, String keyWord, Integer draw, Integer start, Integer length) {
        int pageNum = getPageNum(start, length);

        Map<String, Object> result = DataTableFactory.fitting(draw, codeDictService.pageByParams(type, keyWord, pageNum, length));

        WebUtil.printJson(response, result);
    }

    @RequestMapping("/save")
    @ResponseBody
    public Map<String, Object> save(HttpServletRequest request, Integer id, String type, String code, String name, String unit) {
        return codeDictService.saveInfo(request, id, type, code, name, unit);
    }

    @RequestMapping("/del")
    @ResponseBody
    public Map<String, Object> del(HttpServletRequest request, Integer id) {
        return codeDictService.deleteById(request, id);
    }

    @RequestMapping("/batchDel")
    @ResponseBody
    public Map<String, Object> batchDel(HttpServletRequest request, String ids) {
        return codeDictService.batchDel(request, ids);
    }

    @RequestMapping("/batchUpdate")
    @ResponseBody
    public Map<String, Object> batchUpdate(HttpServletRequest request, String type, String codeDictCodes, String unit) {
        return codeDictService.batchUpdate(request, type, codeDictCodes, unit);
    }

    @RequestMapping("/sync")
    @ResponseBody
    public Map<String, Object> sync(HttpServletRequest request) {
        return codeDictService.syncData(request);
    }
}
